package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.InterventionMapper;
import com.itheima.pojo.Exercise;
import com.itheima.pojo.Food;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.InterventionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service(interfaceClass = InterventionService.class)
@Transactional
public class InterventionServiceImpl implements InterventionService {
    @Autowired
    private InterventionMapper interventionMapper;


    //根据电话号码查找套餐信息
    @Override
    public List<Map> findByTelephone(String telephone) {

       List<Map> list= interventionMapper.findByTelephone(telephone);
        for (Map map : list) {
            Date orderDate = (Date) map.get("orderDate");
            String format = new SimpleDateFormat("yyyy/MM/dd").format(orderDate);
            map.put("orderDate",format);
        }
       return list;
    }
    //根据日期和套餐名称模糊查询套餐信息
    @Override
    public List<Map> findAllByCondition(String before, String after, String queryString,String telephone) {
        List<Map> list = interventionMapper.findAllByCondition(after, before, queryString, telephone);
        for (Map map : list) {
            Date orderDate = (Date) map.get("orderDate");
            String format = new SimpleDateFormat("yyyy/MM/dd").format(orderDate);
            map.put("orderDate",format);
        }
        return list;
    }

    //根据套餐id查询
    @Override
    public Map<String, Object> findById(Integer id,String orderDate,String telephone) {
        Map<String,Object> map=interventionMapper.findById(id,orderDate,telephone);
        //根据套餐查询订单id
      int orderId=interventionMapper.findOrderIdBySetmealId(id,orderDate,telephone);
        //根据订单id查询对应的健康师id

       int doctorId=interventionMapper.findDoctorIdByOrderId(orderId);
       if(!(doctorId>0)){
           return null;
       }
        //根据健康师id查询建议的食物信息
       List<Food> foodList= interventionMapper.findFoodNameByDoctorId(doctorId);
        //根据健康师id查询建议的运动信息
        List<Exercise> exerciseList= interventionMapper.findExerciseNameByDoctorId(doctorId);
        map.put("foodList",foodList);
        map.put("exerciseList",exerciseList);
        /*Date orderDate1 = (Date) map.get("orderDate");
        String format = new SimpleDateFormat("yyyy/MM/dd").format(orderDate1);
        map.put("orderDate",format);*/
        return map;
    }

    @Override
    public Map<String, Object> findOrderById(Integer id, String orderDate, String telephone) {

        Map<String,Object> map= interventionMapper.findOrderById(id,orderDate,telephone);
        Date orderDate1 = (Date) map.get("orderDate");
        String format = new SimpleDateFormat("yyyy/MM/dd").format(orderDate1);
        map.put("orderDate",format);
        return map;

    }
}
